using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class RopeManager : MonoBehaviour
{
    public static RopeManager instance;
    public int[] nodeCountArr;
    public GameObject[] head;
    public GameObject candy;
    private Rigidbody2D preRb;
    public Material lineMaterial;
    private int currIndex = 0;
    public string beCutLineName;

    public int cutIndex;

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
    }

    private void Start()
    {
        for (currIndex = 0; currIndex < head.Length; currIndex++)
        {
            preRb = head[currIndex].GetComponent<Rigidbody2D>();
            if (preRb != null)
            {
                for (int i = 0; i < nodeCountArr[currIndex]; i++)
                {
                    GenerateRope(i);
                }
                head[currIndex].AddComponent<LineManager>();
                HingeJoint2D joint = candy.AddComponent<HingeJoint2D>();
                Candy candyCs = candy.GetComponent<Candy>();
                candyCs.fixs.Add(head[currIndex].transform);
                joint.connectedBody = preRb;
                joint.autoConfigureConnectedAnchor = false;
                joint.connectedAnchor = new Vector2(-0.3f, 0);
            }
        }
    }

    private void GenerateRope(int index)
    {
        GameObject node = new GameObject("Node" + index);
        // node不添加精灵渲染器，没有手动设置渲染层级，代码也没改，默认放在了default？
        // transform？
        node.tag = "Joint";
        node.transform.SetParent(head[currIndex].transform, false);
        HingeJoint2D joint = node.AddComponent<HingeJoint2D>();
        joint.connectedBody = preRb;
        preRb = node.GetComponent<Rigidbody2D>();
        Rigidbody2D rb = node.GetComponent<Rigidbody2D>();
        CircleCollider2D circle = node.AddComponent<CircleCollider2D>();
        circle.isTrigger = true;
        rb.mass = 5;
        rb.drag = 0.2f;
        rb.angularDrag = 0.2f;
        if (index == 0)
            return;
        joint.autoConfigureConnectedAnchor = false;
        joint.connectedAnchor = new Vector2(-0.3f, 0);
    }

    private void removeHingeJoint2D()
    {
        for (int headIndex = 0; headIndex < head.Length; headIndex++)
        {
            string headTransformName = head[headIndex].transform.name;
            if (beCutLineName == headTransformName)
            {
                // 获取正确的head的transform
                Transform headTs = head[headIndex].transform;
                // 获取被切割的节点
                Transform beCutNodeTs = headTs.GetChild(cutIndex);
                // 移除单个
                beCutNodeTs.GetComponent<HingeJoint2D>().enabled = false;
                // 移除所有节点
                // int nodeCount = headTs.childCount;
                // for (int i = 0; i < nodeCount; i++)
                // {
                //     Debug.Log("i");
                //     Debug.Log(i);
                //     Debug.Log("cutIndex");
                //     Debug.Log(cutIndex);
                //     if (i < cutIndex)
                //         return;
                //     Transform node = headTs.GetChild(i);
                //     Destroy(node.gameObject);
                // }
            }
        }
    }

    private void removeCandyHingeJoint2D()
    {
        HingeJoint2D[] joints = candy.transform.GetComponents<HingeJoint2D>();
        // 遍历所有 HingeJoint2D 组件，并将它们销毁
        foreach (HingeJoint2D joint in joints)
        {
            Destroy(joint); // 移除组件
        }
        for (int headIndex = 0; headIndex < head.Length; headIndex++)
        {
            // 获取正确的head的transform
            Transform headTs = head[headIndex].transform;
            LineRenderer line = headTs.GetComponent<LineRenderer>();
            if (line != null)
            {
                line.positionCount = line.positionCount - 1;
                // Debug.Log("line.positionCount");
                // Debug.Log(line.positionCount);
            }
        }
    }
}
